package com.example.brazil_project.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.example.brazil_project.parameter.Parameter;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SQLiteDatabaseUtil {

	public static boolean isCreateSystemInform = false;
	 public static void onCreateTable(String tablename){
		 try {
			 Activity Contx =  Parameter.openingContx;
		
		//打开或创建test.db数据库  
	        SQLiteDatabase db = Contx.openOrCreateDatabase("buy.db", Contx.MODE_PRIVATE, null);  
	        
	        if(tablename.lastIndexOf("SystemInform_table")>0){
	        	 db.execSQL("DROP TABLE IF EXISTS SystemInform_table");
	        }
	         
	        if(tablename.lastIndexOf("ArriveInform_table")>0){
	        	 db.execSQL("DROP TABLE IF EXISTS ArriveInform_table");
	        }
	        if(tablename.lastIndexOf("ArriveProduct_table")>0){
	        	 db.execSQL("DROP TABLE IF EXISTS ArriveProduct_table");
	        }
	     
	        //创建person表  
	       // db.execSQL("CREATE TABLE test (id INTEGER PRIMARY KEY , title VARCHAR, content VARCHAR)"); 
	        
	        db.execSQL(tablename); 
	        isCreateSystemInform = true;
		 } catch (Exception e) {
				// TODO: handle exception
			}
	 }
	 
	 public static void insert(String sql){
		 try {
			 Activity Contx =  Parameter.openingContx;	
		 SQLiteDatabase db = Contx.openOrCreateDatabase("buy.db", Contx.MODE_PRIVATE, null);  
		// db.execSQL("sql", null);
		 // db.execSQL("INSERT INTO test VALUES (2, ?, ?)", new Object[]{"title", "content"});  
		 db.execSQL(sql);
		 } catch (Exception e) {
				// TODO: handle exception
			}
	 }
	 
	 public static Cursor select(String sql){
		 Cursor c = null;
		 try {
			 Activity Contx =  Parameter.openingContx;
		 SQLiteDatabase db = Contx.openOrCreateDatabase("buy.db",Contx.MODE_PRIVATE, null);
		  c = db.rawQuery(sql,null );  
//	        while (c.moveToNext()) {  
//	            int id = c.getInt(c.getColumnIndex("id"));  
//	            String title = c.getString(c.getColumnIndex("title"));  
//	            String content = c.getString(c.getColumnIndex("content"));  
//	            Log.i("db", "_id=>" + id + ", title=>" + title + ", content=>" + content);  
//	        }  
//	        c.close();  
		 } catch (Exception e) {
				// TODO: handle exception
			}
		return c;
		 
	 }
	 
	 
	 
	 /**
	  * 根据查询游标返回list
	  * @param c
	  * @return
	  */
	 public  static List getListbyCursor(Cursor c){
		 List List = new ArrayList<Map>();
		 if(c == null){
			 return null;
		 }
		 while (c.moveToNext()) {
			 Map map = new HashMap<String, String>();
			 String[] cname = c.getColumnNames();
			 for (String string : cname) {
				  
			  	   String value ="";
				   value = c.getString(c.getColumnIndex(string))+""; 
				   map.put(string,value);
				   
			  }
			 List.add(map);
			
		}
		 c.close();
		return List;
		 
	 }
}